package com.cha.chaApp.sys.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cha.chaApp.sys.goods.service.GoodsService;
import com.cha.chaApp.sys.order.dao.ShopCarDao;

import com.cha.chaApp.sys.order.param.CreateShopParam;
import com.cha.chaApp.sys.order.service.ShopCarService;
import com.cha.chaApp.sys.order.vo.ShopCarVO;
import com.cha.chaCommon.core.common.PageParam;
import com.cha.chaCommon.core.entity.hkf.Goods;
import com.cha.chaCommon.core.entity.hkf.Order;
import com.cha.chaCommon.core.entity.hkf.ShopCar;
import com.cha.chaCommon.core.utils.UserUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/*
@Author  hkf
@date 2024/11/14
--轻舟已过万重山--
*/
@Service
public class ShopCarServiceImpl implements ShopCarService {

    @Resource
    private ShopCarDao shopCarDao;

    @Resource
    private GoodsService goodsService;



    @Override
    public void createShopCar(List<CreateShopParam> params) {
        //需要判断用户之前购物车的内容,
        //先将数据库之前的购物车信息清空
        //获得用户的id
        Integer uid = UserUtils.getUid();
        //从数据库中查该用户的购物车
        List<ShopCar> orders = shopCarDao.selectList(new LambdaQueryWrapper<ShopCar>()
                .eq(ShopCar::getUid, uid));
        List<Integer> ids = orders.stream().map(item -> item.getId()).collect(Collectors.toList());
        if(ids.size()>0|| !ids.isEmpty()){
            shopCarDao.deleteBatchIds(ids);
        }
        //根据商品id查到商品信息,计算总金额
        params.stream().forEach((item)-> {
            ShopCar shopCar = new ShopCar();
            Goods goods = goodsService.goodsInfoById(item.getGoodsId());
            BigDecimal mount = BigDecimal.valueOf(item.getOrderNum());
            //设置数量
            shopCar.setGoodsNum(item.getOrderNum());
            //计算总金额
            BigDecimal price = goods.getGoodsMount();
            //设置总金额
            shopCar.setCountMoney(price.multiply(mount));
            //设置用户id
            shopCar.setUid(uid);
            //设置商品id
            shopCar.setGoodsId(item.getGoodsId());
            shopCarDao.insert(shopCar);
        });

    }

    @Override
    public List<ShopCar> shopCarInfoByUid(Integer uid) {

        List<ShopCar> shopCars = shopCarDao.selectList(new LambdaQueryWrapper<ShopCar>()
                .eq(ShopCar::getUid, uid));
        return shopCars;
    }

    @Override
    public Page<ShopCarVO> shopCarVOInfo(PageParam param) {
        Page<ShopCar> page = new Page<>(param.getPageNum(), param.getPageSize());
        Integer uid = UserUtils.getUid();
        Page<ShopCarVO> shopCarVOPage = shopCarDao.selectShopCarVO(page, uid);
        return shopCarVOPage;
    }

    @Override
    public void removeShopCar(Integer uid) {
        shopCarDao.delete(new LambdaUpdateWrapper<ShopCar>()
                .eq(ShopCar::getUid,uid));
    }
}
